package tgipl3.medium.buildingBlocks.minified;
import java.io.IOException;
import java.util.Arrays;
public class UserMainCode
{
	static int output1[], p[][], w[][], z = 0, l, b, i;
	
	static boolean c(int r, int c, int m) {
		if(r < 0 || r >= l || c < 0 || c >= b)
			return true;
		m = Math.max(m - p[r][c], 0);
		if(w[r][c] > m) {
			output1[z] -= w[r][c] - m;
			w[r][c] = m;
			return c(r - 1, c, m + p[r][c]) && c(r, c - 1, m + p[r][c]) && c(r, c + 1, m + p[r][c]) && c(r + 1, c, m + p[r][c]);
		}
		return true;
	}
	
	static void GetWaterLevel(int k, String s)
	{
		output1 = new int[k];
		for(String t : s.substring(1, s.length() - 1).split("\\),\\(")) {
			String r[] = t.substring(t.indexOf(",{{") + 3, t.length() - 2).split("\\},\\{");
			p = new int[l = r.length][b = r[0].split(",").length];
			w = new int[l][b];
			output1[z] = 9 * l * b;
			for(i = 0; i < l * b; i++) {
				p[i / b][i % b] = Integer.parseInt(r[i / b].split(",")[i % b]);
				w[i / b][i % b] = 9;
			}
			for(i = 0; i < l + b; i++)
				if(c(0, i, 0) && c(l - 1, i, 0) && c(i, 0, 0) && c(i, b - 1, 0));
			z++;
		}
	}
	
	public static void main(String[] args) throws IOException
	{
		String[] inputs = "1:(3,6,{{3,3,4,4,4,2},{3,1,3,2,1,4},{7,3,1,6,4,1}})".split(":");
		GetWaterLevel(Integer.parseInt(inputs[0]), inputs[1]);
		System.out.println(Arrays.toString(output1));
	}
}